package com.example.administrator.bdsjws.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.administrator.bdsjws.R;
import com.example.administrator.bdsjws.fragment.base.BaseFragment;
import com.example.administrator.bdsjws.services.MyDownLoadService;
import com.example.administrator.bdsjws.vo.VersionInfo;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2017/5/17.
 */

public class UpdateVersionFragment extends BaseFragment implements View.OnClickListener{

    Unbinder unbinder;
    @BindView(R.id.tv_update)
    TextView dialogfragment;
    @BindView(R.id.jujue)
    Button jujue;
    @BindView(R.id.xiazai)
    Button xiazai;
    public String getMsg(){
        Bundle bundle1=getArguments();
        String msg=bundle1.getString("msg");
        return msg;
    }

    @Override
    public void initDateView(View view) {
        //更新TextView   getArgument
        Bundle bundle=getArguments();
        VersionInfo versionInfo= (VersionInfo) bundle.getSerializable("versioninfo");
        jujue.setOnClickListener(this);
        xiazai.setOnClickListener(this);
        dialogfragment.setText("下载更新");//versionInfo.getInfo());
        if(versionInfo!=null){
            dialogfragment.setText(versionInfo.getInfo());
        }else{
            jujue.setVisibility(View.GONE);
            dialogfragment.setText(getMsg());
        }
    }

    @Override
    public int getContextXml() {
        return R.layout.update_version_dialog;//缺少布局文件
    }
    //写一个方法构造UpdateVersionFragment对象  方法的重载
    public static UpdateVersionFragment getUpdateVersionFragment(String msg) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putString("msg",msg);
        updateVersionFragment.setArguments(bundle);//设置值   传递给Activity
        return updateVersionFragment;
    }
    //写一个方法构造UpdateVersionFragment对象
    public static UpdateVersionFragment getUpdateVersionFragment(VersionInfo versionInfo) {
        UpdateVersionFragment updateVersionFragment = new UpdateVersionFragment();
        Bundle bundle = new Bundle();
        bundle.putSerializable("versioninfo", versionInfo);
        updateVersionFragment.setArguments(bundle);//设置值   传递给Activity
        return updateVersionFragment;
    }

//    @Override
//    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//        // TODO: inflate a fragment view
//        View rootView = super.onCreateView(inflater, container, savedInstanceState);
//        unbinder = ButterKnife.bind(this, rootView);
//        return rootView;
//    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.jujue:
                dismiss();
                break;
            case R.id.xiazai:
                Bundle bundle=getArguments();
                VersionInfo versionInfo= (VersionInfo) bundle.get("versioninfo");
                Log.d("aaaaaa",versionInfo.getApkUrl());
                if(versionInfo!=null){
                    Intent intent=new Intent();
                    intent.setClass(getActivity(), MyDownLoadService.class);
                    intent.putExtra("apkUrl",versionInfo.getApkUrl());
                    getActivity().startService(intent);
                }
                dismiss();
                break;
        }
    }
}